﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace sieuthi
{
    public partial class nhanvien : Form
    {
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        SqlConnection conn = new SqlConnection(Threelayer.VO.core.connectionsring.ToString());
        DataTable dt = new DataTable("nhanvien");
        Threelayer.BUS.nhanvienBUS nv_bus;
        Threelayer.VO.nhanvienVO nv_et;
        public nhanvien()
        {
            InitializeComponent();
        }
        private void nhanvien_Load(object sender, EventArgs e)
        {
            this.vIEWnhanvienTableAdapter1.Dispose();
            // TODO: This line of code loads data into the 'sieuthiDataSet2.VIEWnhanvien' table. You can move, or remove it, as needed.
            this.vIEWnhanvienTableAdapter1.Fill(this.sieuthiDataSet2.VIEWnhanvien);
            nhanviendatabind();
            
        }
        private void nhanviendatabind()
        {
            box_luong.DataBindings.Clear();
            box_luong.DataBindings.Add("text", dataGridkhachhang.DataSource, "luong");
            box_magh.DataBindings.Clear();
            box_magh.DataBindings.Add("text", dataGridkhachhang.DataSource, "magh");
            box_manv.DataBindings.Clear();
            box_manv.DataBindings.Add("text", dataGridkhachhang.DataSource, "manv");
            //box_ngaysinh.DataBindings.Clear();
            //box_ngaysinh.DataBindings.Add("date", dataGridkhachhang.DataSource, "ngaysinh");
            box_quequan.DataBindings.Clear();
            box_quequan.DataBindings.Add("text", dataGridkhachhang.DataSource, "quequan");
            box_sdt.DataBindings.Clear();
            box_sdt.DataBindings.Add("text", dataGridkhachhang.DataSource, "sdt");
            box_tennv.DataBindings.Clear();
            box_tennv.DataBindings.Add("text", dataGridkhachhang.DataSource, "ten");
            box_nhanvien_timkiem.DataBindings.Clear();
            box_nhanvien_timkiem.DataBindings.Add("text", dataGridkhachhang.DataSource, "manv");
            
        }
        private void getdata()
        {
            dt.Clear();
            cmd.Dispose();
            cmd.Parameters.Clear();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            cmd.CommandText = "nhanvien_xem";
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.ExecuteNonQuery();
            da.Fill(dt);
            dataGridkhachhang.DataSource = dt;
            conn.Close();
        }
        private void data_nhanvien()
        {
            nv_et = new Threelayer.VO.nhanvienVO();
            nv_et.Luong = Convert.ToInt32(box_luong.Text);
            nv_et.maGH = box_magh.Text;
            nv_et.MaNV = box_manv.Text;
            nv_et.ngaysinh =Convert.ToString( box_ngaysinh.Text);
            nv_et.quequan = box_quequan.Text;
            nv_et.Sdt = Convert.ToInt32(box_sdt.Text);
            nv_et.ten = box_tennv.Text;
        }
        private void bt_nhanvien_them_Click(object sender, EventArgs e)
        {
            try
            {
                data_nhanvien();
                nv_bus = new Threelayer.BUS.nhanvienBUS();
                nv_bus.nhanvien_them(nv_et);
                getdata();
            }
            catch
            {
                MessageBox.Show("Not alow null","Lỗi",MessageBoxButtons.OK);

            }
            
        }

        private void bt_nhanvien_sua_Click(object sender, EventArgs e)
        {
            try
            {
                data_nhanvien();
                nv_bus = new Threelayer.BUS.nhanvienBUS();
                nv_bus.nhanvien_sua(nv_et);
                getdata();
            }
            catch
            {
                MessageBox.Show("Not alow null", "Lỗi", MessageBoxButtons.OK);

            }
        }

        private void bt_nhanvien_xoa_Click(object sender, EventArgs e)
        {
            dt.Clear();
            cmd.Dispose();
            cmd.Parameters.Clear();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            cmd.CommandText = "nhanvien_xoa";
            cmd.Parameters.Add("@manv", SqlDbType.NVarChar, 200).Value = box_nhanvien_timkiem.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.ExecuteNonQuery();
            da.Fill(dt);
            dataGridkhachhang.DataSource = dt;
            conn.Close();
        }

        private void bt_nhanvien_timkiem_Click(object sender, EventArgs e)
        {
            dt.Clear();
            cmd.Dispose();
            cmd.Parameters.Clear();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            cmd.CommandText = "nhanvien_timkiem";
            cmd.Parameters.Add("@manv", SqlDbType.NVarChar, 200).Value = box_nhanvien_timkiem.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.ExecuteNonQuery();
            da.Fill(dt);
            dataGridkhachhang.DataSource = dt;
            conn.Close();
            
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            getdata();
        }

        
    }
}
